package com.example.team.dao.Imp;

import com.example.team.dao.IOffdutyDao;
import com.example.team.entity.Offduty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 作者:王轩
 * date: 2023/10/30
 * 销假管理
 **/
@Repository
public class IOffdutyDaoImpl implements IOffdutyDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertOffduty(Offduty offduty) {
        return jdbcTemplate.update("insert into offduty(OffDutyID,LeaveID,TripID,EmployeeID,DisruptDate,EndDate)"+"value(?,?,?,?,?,?)",
                offduty.getOffDutyID(),
                offduty.getLeaveID(),
                offduty.getTripID(),
                offduty.getEmployeeID(),
                offduty.getDisruptDate(),
                offduty.getEndDate());
    }

    @Override
    public int deleteOffduty(Offduty offduty) {
        return jdbcTemplate.update("delete from offduty where OffDutyID=?",offduty.getOffDutyID());
    }

    @Override
    public int updateOffduty(Offduty offduty) {
        return jdbcTemplate.update("update offduty set LeaveID=?,TripID=?,EmployeeID=?,DisruptDate=?,EndDate=? where OffDutyID=?",
                offduty.getLeaveID(),
                offduty.getTripID(),
                offduty.getEmployeeID(),
                offduty.getDisruptDate(),
                offduty.getEndDate(),
                offduty.getOffDutyID());
    }

    @Override
    public List<Offduty> getAllOffduty(Offduty offduty) {
        return jdbcTemplate.query("select * from offduty",new Object[]{},
                new BeanPropertyRowMapper<>(Offduty.class));
    }

    @Override
    public Offduty getOffduty(Offduty offduty) {
        List<Offduty> result=jdbcTemplate.query("select " +
                        "*" +
                        "from offduty where OffdutyID='"+offduty.getOffDutyID()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Offduty.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
